package codegen;

import codegen.config.CommonCustomConfig;
import codegen.constant.GenTypeEnum;
import codegen.generator.IndexHtmlGenerator;
import codegen.generator.IndexJsGenerator;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.generator.GeneratorFactory;
import com.zaxxer.hikari.HikariDataSource;
import com.zzz.framework.config.mybatis.BaseMapperX;
import com.zzz.framework.model.BaseDO;

public class Codegen {

    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/codegen?characterEncoding=utf-8");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle2();

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfigUseStyle2() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //设置根包
        globalConfig.getPackageConfig()
                .setBasePackage("com.zzz.demo");

        //设置表前缀和只生成哪些表，setGenerateTable 未配置时，生成所有表
        globalConfig.getStrategyConfig()
                .setTablePrefix("tb_")
                .setIgnoreColumns("id", "remark", "creator", "create_time", "updater", "update_time", "deleted", "deleted_time")
                .setGenerateTable("system_dict_type", "system_dict_data");

        //设置生成 entity 并启用 Lombok
        globalConfig.enableEntity()
                .setSuperClass(BaseDO.class)
                .setWithLombok(true)
                .setJdkVersion(8);

        //设置生成 mapper
        globalConfig.enableMapper()
                .setSuperClass(BaseMapperX.class);
        //设置生成 xml
        globalConfig.enableMapperXml();

        //设置生成 service
        globalConfig.enableService();
        globalConfig.enableServiceImpl();

        //设置生成 controller
        globalConfig.enableController();
        globalConfig.getTemplateConfig().setController("/tpl/v1/controller.tpl");

        //自定义模版1
        GeneratorFactory.registerGenerator(GenTypeEnum.INDEX_HTML.getType(), new IndexHtmlGenerator());
        globalConfig.getCustomConfig().put(GenTypeEnum.INDEX_HTML.getType(), CommonCustomConfig.builder()
                .genEnable(true).classSuffix("Index").prefixDir("system")
                .build());

        //自定义模版2
        GeneratorFactory.registerGenerator(GenTypeEnum.INDEX_JS.getType(), new IndexJsGenerator());
        globalConfig.getCustomConfig().put(GenTypeEnum.INDEX_JS.getType(), CommonCustomConfig.builder()
                .genEnable(true).classSuffix("Index").prefixDir("system")
                .build());

        //可以单独配置某个列
//        ColumnConfig columnConfig = new ColumnConfig();
//        columnConfig.setColumnName("tenant_id");
//        columnConfig.setLarge(true);
//        columnConfig.setVersion(true);
//        globalConfig.getStrategyConfig()
//                .setColumnConfig("tb_account", columnConfig);

        return globalConfig;
    }
}
